package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.zhidao.model.po.Admin;
import com.example.zhidao.model.po.Classify;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.service.AdminService;
import com.example.zhidao.service.ClassifyService;
import com.example.zhidao.utils.Ujwt;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/classify")
public class ClassifyController {//4

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private Ujwt ujwt;

    @Autowired
    private AdminService adminService;

//    获取分类
    @GetMapping
    public ResponseData selallcl(){
        List<Classify> cs=classifyService.list();

        if(cs==null){
            return new ResponseData(cs,false,"查询失败",200);
        }else{
            return new ResponseData(cs,true,"查询成功",200);
        }
    }








//    下面为管理员的接口
//    ---------------------------------------------------------------------------------------------------------


//    添加分类
    @PostMapping
    public ResponseData insertcl(@RequestHeader String token,String name){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }

        QueryWrapper<Classify> qw=new QueryWrapper<>();
        qw.eq("name",name);

        Classify classifies=classifyService.getOne(qw);

        if(classifies!=null){
            return new ResponseData(null,false,"改类型已经存在",202);
        }

        Classify cl=new Classify();
        cl.setName(name);

        boolean i=classifyService.save(cl);

        if(i){
            return new ResponseData(i,true,"添加成功",200);
        }else{
            return new ResponseData(i,false,"添加失败",201);
        }
    }

//    删除分类
    @DeleteMapping
    public ResponseData delcl(@RequestHeader String token,int id){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }

        boolean i=classifyService.delClassify(id);

        if(i){
            return new ResponseData(i,false,"删除成功",200);
        }else{
            return new ResponseData(i,false,"删除失败",201);
        }
    }

    //    修改分类分类
    @PutMapping
    public ResponseData updatecl(@RequestHeader String token,@RequestBody Classify classify){


        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }

        UpdateWrapper<Classify> uw=new UpdateWrapper<>();
        uw.eq("id",classify.getId());
       uw.set("name",classify.getName());

        boolean i=classifyService.update(uw);

        if(i){
            return new ResponseData(i,true,"修改成功",200);
        }else{
            return new ResponseData(i,false,"修改失败",201);
        }
    }

}
